var ns,toast;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        showtime:false,
        ProjectName:'',
        ReportType:0,//0 设备巡检  1 安防巡更  2 环境保洁
        form: {
            startMonth: '', 
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
        },
        summary: {},
        projectList: [], 
        CheckData:{},  ////检查任务
        QualityCheckData:{},  ////考核任务
        QualityControlData:[],  ////整改任务
        QuaLityControl:[],//问题分类
        zhenggaiData:[],
        parseArea:0,//考核任务完成率
        totalNum:0,//整改任务总数
    },
    methods: {
        get_data: function () {
            var that = this;
            that.getSearchParams();
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
            that.showsearchparam = false;
            that.doSearch();
        },
        //巡检任务
        do_openxunjian:function(){
            var that=this;
            ns.openWin('quality_analysis_xunjian','品质巡查任务',{
                ProjectID:that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
            });
        },
        //整改任务
        do_openzhenggai:function(){
            var that=this;
            ns.openWin('quality_analysis_zhenggai','整改任务',{
                ProjectID:that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
            });
        },
        //筛选数据
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        onShowTime:function(IsShow){
            var that=this;
            that.showtime=IsShow;
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            // if (type == 2 && that.showsearchmonth) {
            //     setTimeout(function () {
            //         that.initPicker();
            //     }, 100);
            // }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        getAnalysisData:function(){
            var that=this;
            that.CheckData={};  ////检查任务
            that.QualityCheckData={}; ////考核任务
            that.QualityControlData=[];  ////整改任务
            that.QuaLityControl=[];//问题分类
            var options = {};
            options.action = "getqualitycontrol"; ////方法名称
            options.ProjectID = that.form.ProjectID;
            options.DayTime = that.form.startMonth;
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function(succeed, data, err) {
                toast.hide();

                if(succeed){
                    if(data.status){
                        that.CheckData=data.CheckData;
                        that.QualityCheckData=data.QualityCheckData;
                        that.QualityControlData=data.QualityControlData;
                        that.QuaLityControl=data.QuaLityControl;
                        // 考核任务完成率
                        var a=that.QualityCheckData.EndCount;
                        var b=that.QualityCheckData.AllCount;
                        if(a>0&&b>0){
                            that.parseArea=((a/b)*100).toFixed(2);
                        }else{
                            that.parseArea=0.00;
                        }

                        //整改任务
                        that.totalNum=0;
                        for (var i = 0; i < that.QualityControlData.length; i++){
                            that.totalNum=that.totalNum+that.QualityControlData[i].y;
                        }
                        setTimeout(function() {
                            if(that.totalNum>0){
                                that.loadzgchart(that.QualityControlData);
                            }
                        }, 300)
                        

                        

                        setTimeout(function() {
                            if(that.QuaLityControl.length>0){
                                that.loadChart(that.QuaLityControl);
                            }
                        }, 300)
                        
                        
                    }else{
                        api.toast({
                            msg: '数据错误',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });

        },
        loadzgchart:function(zglist){
            var chart = new Highcharts.Chart('zgcontainer',{
                chart: {
                    // backgroundColor: '#FCFFC5',
                    spacing : [0, 0 , 0, 0],
                    marginLeft:-100,
                    marginTop:10,
                    marginRight:0,
                    marginBottom:0,

                    // 指定图表大小
                    width: 300,
                    height: 200
                },
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                title: {
                    floating:true,
                    text:'',
                    align: 'left',
                    x: 90,
                    y: 30,
                    style: {
                        fontSize: '24px'
                    }
                },
                credits: {
                    enabled: false //不显示LOGO
                },
                // tooltip: {
                //     pointFormat: '{series.name}: <b>{series.y}</b>'
                // },
                legend:{
                    align:'left',
                    verticalAlign: 'top',
                    floating: true,
                    x: 200,
                    y: 30,
                    labelFormat: '{name} {y}',
                    itemMarginTop: 8,
                    itemMarginBottom: 8,
                    itemStyle:{
                        fontWeight:400,
                    }
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: false,
                            // format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                            // style: {
                            //     color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            // }
                        },
                        showInLegend: true,
                        point: {
                            // events: {
                            //     mouseOver: function(e) { 
                            //         chart.setTitle({
                            //             text: e.target.name+ '\t'+ e.target.y + ' %'
                            //         });
                            //     }
                            // }
                        },
                    }
                },
                series: [{
                    name: '数量',
                    type: 'pie',
                    innerSize: '80%',
                    data: zglist
                }]
            }, function(c) { // 图表初始化完毕后的会掉函数
                // 环形图圆心
                var centerY = c.series[0].center[1],
                    titleHeight = parseInt(c.title.styles.fontSize);
                // 动态设置标题位置
                c.setTitle({
                    y:centerY + titleHeight/2,
                    // text:c.series[0].data[4].y
                });
            })

        },

        loadChart: function(list) {
            var chart = new Highcharts.Chart('container',{
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    type: 'pie',
                    margin:null,
                },
                colors: ['#2d94e7', '#28ca59', '#a734e2', '#e5db39',
				 '#e9a119', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'],
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                title: {
                    text: ''
                },
                credits: {
                    enabled: false //不显示LOGO
                },
                
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                    
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '{point.name}: <br/>{point.Count}',
                            style: {
                                with:'20px',
                                fontSize:'9px',
                                fontWeight:'400',
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || '#666'
                            }
                        }
                    }
                },
                series: [{
                    name: '占比',
                    colorByPoint: true,
                    data: list
                }]
            }, function(c) { // 图表初始化完毕后的会掉函数
            });
        },
  
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //月份
                    var btn = document.querySelector('#datemonth');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.startMonth){
                                _self.picker.setSelectedValue(_this.form.startMonth);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.startMonth = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.startMonth){
                                _self.picker.setSelectedValue(_this.form.startMonth);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startMonth = rs.value;
                                    _this.doSearch();
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    var nowDate = new Date();
    var Y = nowDate.getFullYear();
    var M = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    app.form.startMonth = Y+'-'+M;//初始化默认当前月
    app.ReportType=api.pageParam.servicetype || 0;
    app.initPicker();
    app.get_data();

};
